/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Hai-PC
 */
@ManagedBean(name = "staffLoginController")
@SessionScoped
public class StaffLoginController extends AbstractController {

    private String username = "";
    private String password = "";

    public boolean isLoggedIn()
    {
        if (username.equals("admin") && password.equals("admin"))
        {
            return true;
        }
        return false;
    }

    /**
     * LogIn Staff for Web
     *
     * @return 
     */
    public String login() {
        String outcome = null;
        try
        {
            if (username.isEmpty() || password.isEmpty())
            {
                addErrorMessage("Tên đăng nhập hoặc mật khẩu rỗng.");
            }
            else
            {
                if (!username.equals("admin"))
                {
                    addErrorMessage("Tên đăng nhập sai.");
                }
                else
                {
                    if (!password.equals("admin"))
                    {
                        addErrorMessage("Mật khẩu sai.");
                    }
                    else
                    {
                        outcome = "dashboard";
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.toString());
        }
        return outcome;
    }

    /**
     * Logout Staff for Web
     *
     */
    public void logout() {
        username="";
        password="";
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

        FacesContext facesContext = FacesContext.getCurrentInstance();
        NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
        nh.handleNavigation(facesContext, null, "login");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}